package cn.softgame.jserver.queue;

import java.util.LinkedList;

public class MessageQueue {

	private static MessageQueue instance = new MessageQueue();
	
	private LinkedList<Object> requestList = new LinkedList<Object>();
	private LinkedList<Object> responseList = new LinkedList<Object>();
	
	public static MessageQueue getInstance(){
		return instance;
	}
	
	public void pushRequest(Object requestMsg){
		synchronized(requestList){
			requestList.add(requestMsg);
			requestList.notifyAll();
		}
	}
	
	public void pushResponse(Object responseMsg){
		synchronized(responseList){
			responseList.add(responseMsg);
			responseList.notifyAll();
		}
	}
	
	public Object removeReqFirst(){
		synchronized(requestList){
			while(requestList.isEmpty()){
				try {
					requestList.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return requestList.removeFirst();
		}
	}
	
	public Object removeRespFirst(){
		synchronized(responseList){
			while(responseList.isEmpty()){
				try {
					responseList.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return responseList.removeFirst();
		}
	}
}
